package com.simple.service;

import com.simple.mapper.ocrmark.EsMarkMapper;
import com.simple.mapper.ocrmark.OcrQuestionMarkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EsMarkService {

    @Autowired
    private EsMarkMapper esMarkMapper;
    @Autowired
    private OcrQuestionMarkMapper ocrQuestionMarkMapper;

    @Value("${ocrmark.imgPath}")
    private String imgPath;


    public Map getOneImg(){
        Map map = new HashMap();
        String img = esMarkMapper.getOneImg();
        if(img != null && !"".equals(img)) {
            esMarkMapper.marking(img);
        }else{
            return null;
        }
        map.put("name",img);
        String filePath = imgPath + img;
        if(!new File(filePath).exists()){
            System.err.println(filePath+" 服务器文件不存在........................");
            return null;
        }
        byte[] bytes = getBytes(new File(filePath));
        org.apache.commons.codec.binary.Base64 base64 = new  org.apache.commons.codec.binary.Base64();
        String imgBase64 = base64.encodeToString(bytes);
        map.put("imgBase64","<img src=\"data:image/jpeg;base64," + imgBase64 + "\" width=\"800\" height=\"300\">");
        return map;
    }



    public void updateMark(Map map) {
        esMarkMapper.updateMark(map);
    }



    public void initData() {
        List<Map<String,Object>> data = ocrQuestionMarkMapper.queryIsMarkNoSearch();
        List<String> imgs = new ArrayList<>();
        for(Map<String,Object> map : data){
            imgs.add((String)map.get("img_name"));
        }
        esMarkMapper.insertBatch(imgs);
    }


    public void initDataSql() {
        esMarkMapper.insertInit();
    }

    public void updateDealBatch(List<String> imgs) {
        esMarkMapper.updateDealBatch(imgs);
    }

    public void updateDeal(List<String> imgs) {
        for(String img : imgs){
            esMarkMapper.updateDeal(img);
        }

    }




    public static byte[] getBytes(File file) {
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            // picture max size is 2MB
            ByteArrayOutputStream bos = new ByteArrayOutputStream(2000 * 1024);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }



    public Map getDealEs(){
        Map map = new HashMap();
        map.put("totalCount", esMarkMapper.queryAllCount());
        map.put("isMarkCount",esMarkMapper.queryIsMarkCount());
        map.put("noMarkCount",esMarkMapper.queryNoMarkCount());
        return map;
    }


    public void  unlockMarking(){
        esMarkMapper.unlockMarking();
    }


}

